<?php
namespace app\admin\controller;

use app\model\admin;
use think\Request;
use think\Session;
use think\Controller;

class Sale extends Controller
{
    //商品列表
    public function index()
    {
        $dao = new \app\model\admin\Sale();
        $where = [];//自 5.4 起可以使用短数组定义语法，用 [] 替代 array()
        $title = $this->request->get("title");
        $lists = $dao->get_sale_lists($where,$sort='id desc',$paginate=10);
        $this->assign('lists', $lists);
        return $this->fetch('sale/index');
    }

    //商品详情
    public function detail()
    {
        $dao = new \app\model\admin\Sale();
        $id = $request = $this->request->get('id');
        if(empty($id)){
            $data = array();
        }else{
            $data = $dao->sale_detail($id);
        }
        return $this->fetch('sale/detail',$data);
    }

    //商品保存修改
    public function sale_save()
    {
        $dao = new \app\model\admin\Sale();
        $data['id'] = deletehtml($this->request->post('id'));
        $data['title'] = deletehtml($this->request->post('title'));
        $data['seo_description'] = deletehtml($this->request->post('seo_description'));
        $data['n_price'] = deletehtml($this->request->post('n_price'));
        $data['y_price'] = deletehtml($this->request->post('y_price'));
        $data['pay_type'] = (int)$this->request->post('pay_type');
        $data['status'] = (int)$this->request->post('status');
        $data['description'] = $this->request->post('description');
        $data['instructions'] = $this->request->post('instructions');

        $file = $this->request->file('image_f');
        if(!empty($file)){//如果图片不为空则为修改
            $updimg_status = upload_file($file,'jpg,png,gif','512000','/uploads/sale_img');
            if($updimg_status){ //如果图片上传成功，则把图片路径写到数据库里
                $data['image_f'] = $updimg_status;
            }
        }
        $file = $this->request->file('image_s');
        if(!empty($file)){//如果图片不为空则为修改
            $updimg_status = upload_file($file,'jpg,png,gif','512000','/uploads/sale_img');
            if($updimg_status){ //如果图片上传成功，则把图片路径写到数据库里
                $data['image_s'] = $updimg_status;
            }
        }
        $file = $this->request->file('image_t');
        if(!empty($file)){//如果图片不为空则为修改
            $updimg_status = upload_file($file,'jpg,png,gif','512000','/uploads/sale_img');
            if($updimg_status){ //如果图片上传成功，则把图片路径写到数据库里
                $data['image_t'] = $updimg_status;
            }
        }
        if(empty($data['id'])){
            $data['add_time'] = time();
            $article_status = $dao->add_sale($data);
        }else{
            $data['end_time'] = time();
            $where['id'] = $data['id'];
            $article_status = $dao->update_sale($where,$data);
        }
        if($article_status){
            $this->redirect('/admin/sale/index?');
        }else{
            $this->error('操作失败！', '/admin/sale/detail?id='.$data['id']);
        }
    }
    //新增关联商品
    public function add_relation(){
        if((int)$this->request->get("id")<1){
            $this->redirect('/admin/sale/index?');
        }
        $dao = new \app\model\admin\Sale();
        $data = $dao->sale_detail($this->request->get("id"));
        if(!$data)$this->error('商品不存在！', '/admin/sale/index?');
        $this->assign('data', $data);
        //查询关联地址
        $where['pid'] = $this->request->get("id");
        $lists = $dao->select_sale_relation($where);
        $this->assign('lists', $lists);
        return $this->fetch('sale/relation');
    }
    //添加关联商品
    public function sale_status(){
        if((int)$this->request->post("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }
        $dao = new \app\model\admin\Sale();
        $data['pid'] = $this->request->post("id");
        $data['title'] = $this->request->post("gl_title");
        $data['link'] = $this->request->post("gl_link");
        $status = $dao->add_sale_relation($data);
        if($status)return json(['status'=>0,'data'=>(object)[],'msg'=>'ok!']);
        return json(['status'=>0,'data'=>(object)[],'msg'=>'操作失败!']);
    }
    //删除关联商品
    public function del_sale_relation(){
        if((int)$this->request->post("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }
        $dao = new \app\model\admin\Sale();
        $id = (int)$this->request->post("id");
        $status = $dao->del_sale_relation([$id]);
        if($status == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$status,'msg'=>'操作成功!']);
        }
    }

    //文章删除
    public function page_delete()
    {
        $dao = new \app\model\admin\Page();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $article_delete = $dao->delete_page([$id]);
        if($article_delete == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$article_delete,'msg'=>'操作成功!']);
        }
    }

}


